﻿
using System.Net.Sockets;
using System.Net;
using System;
using System.Text;
using System.Threading;
using System.IO;
using System.Windows.Forms;


namespace WirelessBarcodeReceiver
{
    partial class Form1
    {

        // The address of the multicast group to join.
        // Must be in the range from 224.0.0.0 to 239.255.255.255
        private const string GROUP_ADDRESS = "224.0.1.12";

        // The port over which to communicate to the multicast group
        private const int GROUP_PORT = 54329;

        UdpClient client = new UdpClient();

        Thread thread = null;
        bool threadPleaseExit;


        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(3, 3);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(799, 422);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(719, 394);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "Exit";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox1.Location = new System.Drawing.Point(322, 31);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(472, 53);
            this.textBox1.TabIndex = 3;
            this.textBox1.Text = "none";
            this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(806, 429);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Wireless Barcode Receiver";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private void startUDP()
        {
            IPEndPoint localEp = new IPEndPoint(IPAddress.Any, GROUP_PORT);
            client.Client.Bind(localEp);

            IPAddress multicastaddress = IPAddress.Parse(GROUP_ADDRESS);
            client.JoinMulticastGroup(multicastaddress);

            Console.WriteLine("UDP listening");
            while (!threadPleaseExit)
            {
                byte[] data = client.Receive(ref localEp);
                ProcessReceivedBytes(data);
            }
        }

        public delegate void MyDelegate(string myArg2);

        void ProcessReceivedBytes(byte[] bytes)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            string ret = encoding.GetString(bytes);

            // http://www.codeproject.com/Articles/10311/What-s-up-with-BeginInvoke
            textBox1.BeginInvoke(new MyDelegate(DelegateMethod), ret);
            
        }

        public void DelegateMethod(string myCaption)
        {
            this.textBox1.Text = myCaption;
            Clipboard.SetText(this.textBox1.Text);
        }

        protected void StartThread()
        {
            Console.WriteLine("StartThread");
            threadPleaseExit = false;
            thread = new Thread(startUDP);
            thread.Start();
        }


        protected void StopThread()
        {
            Console.WriteLine("StopThread");
            threadPleaseExit = true;
            if (thread != null)
            {
                Console.WriteLine("Closing...");
                thread.Abort();
                client.Close();
                Console.WriteLine("OK");
            }

        }


        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button button1;
        private TextBox textBox1;
    }
}

